<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/10
 * Time: 15:33
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use common\helps\tools;
use backend\models\GoodsList;

class BannerConfig extends ActiveRecord{

    public $page_num	= 10;		//分页条数
    public $ispage		= 0;			//是否需要分页
    public $nowpage		= 1;			//当前页
    public $ntoArray	= 0;			//查询时候转化为数据
    public $brand_category_id;         //品牌街品牌分类id
    public $type = 1;                  //类型
    public $platform = 1;              //平台1：pc；2wap


	const TYPE_ONE	=1;//热卖商品列表中的数据
	const TYPE_TWO	=2;//热卖商品首页数据

    public static function tableName(){
        return '{{%banner_config}}';
    }

    //add by huangzhao 20180310 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页  start
    public function getBannerStreetList(){
        $params	=	[
            'bc.brand_street_category_id'			=>$this->brand_category_id,
            'hs.type'			=>$this->type,
            'hs.platform'			=>$this->platform,
            'hs.use_flg'			=>HotSaleConfig::USER_FLG_YES
        ];

        $params	= array_filter($params);
		//add FDG367 2018/03/14 ID533 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页 start
        $info	= self::find()->alias('bc')->where($params)->orderBy('hs.sort asc')
            ->leftJoin('j_hot_sale_config as hs','hs.id=bc.group_id')->select('bc.*,hs.brand_id');
		//add FDG367 2018/03/14 ID533 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页 start
        if($this->ispage){
            $info->limit($this->page_num)
                ->offset($this->page_num *($this->nowpage-1));
        }

        if($this->ntoArray){
            $info->asArray();
        }
        return $info->all();
    }
    //add by huangzhao 20180310 534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页  end

    //获取首页banner和品牌街图片
    public function getBannerListByType($s_type){
        $where = ['hc.use_flg'=>1,'hc.type'=>$s_type,'hc.category_id'=>0,'hc.platform'=>1];
        $list = array();
        $result = $this::find()->where($where)->orderBy('hc.sort asc')->alias('bc')->
            leftJoin('j_hot_sale_config as hc','bc.group_id = hc.id')->select('bc.*,hc.type,hc.brand_id,hc.is_show')->asArray()->all();
        if(!empty($result)){
            foreach($result as $value){
                $list[$value['type']]['type'] = $value['type'];
                $list[$value['type']]['list'][] = $value;
            }
        }
        return $list;
    }

    public function getHotGoodsList($type,$platform=1){
        $where = ['use_flg'=>1,'type'=>$type,'platform'=>$platform];

        $groups = HotSaleConfig::find()->where($where)->select('id,type')->asArray()->all();
        if(empty($groups)){
            $group_id = array();
        }else{
            $group_id = array_column($groups,'id');
        }
        $where = ['group_id'=>$group_id];
        $result = $this::find()->where($where)->orderBy('sort asc')->asArray()->all();
//         if(empty($result)){
//             $result = array();
//         }
        $sku = is_array($result) && count($result) > 0 ? array_column($result, 'sku') : [];
        $sku_data = $goods_identification = [];
        if (count($sku) > 0){
            $goods_sale = GoodsSale::find()->select('sku,main_goods_id')->where(['sku'=>$sku])->asArray()->all();
            $main_goods_ids = count($goods_sale) > 0 ? array_filter(array_column($goods_sale, 'main_goods_id')) : [];
            if (count($main_goods_ids) > 0){
                $goods_list = GoodsList::find()->alias('g')
                    ->select('g.main_goods_id,g.goods_name_cn,g.brand_name,g.identification_id,g.series,g.hidden_price,g.market_price,g.sale_price,g.unit,b.country_icon')
                    ->where(['g.main_goods_id'=>$main_goods_ids])->leftJoin('j_goods_brand as b','b.id = g.brand_id')->asArray()->all();
                $identification_id = count($goods_list) > 0 ? array_filter(array_column($goods_list, 'identification_id')) : [];
                $goods_list = array_combine(array_column($goods_list, 'main_goods_id'), $goods_list);
                if (count($identification_id) > 0){
                    $goods_identification = GoodsIdentification::find()->select('id,name')->where(['id'=>$identification_id])->asArray()->all();
                    $goods_identification = array_combine(array_column($goods_identification, 'id'), $goods_identification);
                }
            }
            if (is_array($goods_sale) && count($goods_sale) > 0)foreach ($goods_sale as $goods_sale_val){
                $main_goods_id = $goods_sale_val['main_goods_id'];
                $goods_list_val = tools::arrayHasValue($goods_list,$main_goods_id) ? $goods_list[$main_goods_id] : [];
                $identification_id = tools::arrayHasValue($goods_list_val,'identification_id') ? $goods_list_val['identification_id'] : '';
                $identification = tools::arrayHasValue($goods_list,$main_goods_id) && tools::arrayHasValue($goods_identification,$identification_id) ? $goods_identification[$goods_list_val['identification_id']] : [];
                $sku_data[$goods_sale_val['sku']] = [
                    'main_goods_id'=>$main_goods_id,
                    'goods_name_cn'=>$goods_list_val['goods_name_cn'],
                    'brand_name'=>$goods_list_val['brand_name'],
                    'series'=>$goods_list_val['series'],
                    'hidden_price'=>$goods_list_val['hidden_price'],
                    'market_price'=>$goods_list_val['market_price'],
                    'sale_price'=>$goods_list_val['sale_price'],
                    'unit'=>$goods_list_val['unit'],
                    'country_icon'=>$goods_list_val['country_icon'],
                    'identification'=>tools::arrayHasValue($identification,'name') ? $identification['name'] : ''
                ];
            }
        }
        $goods__list = array();
        if (is_array($result) && count($result) > 0)foreach($result as $value){
            $value['sku_data'] = tools::arrayHasValue($sku_data,$value['sku']) ? $sku_data[$value['sku']] : '';

			//add by FDG367 495 【商城需求】商品图片上增加促销打标 2018-02-02 start
			$value['activity_message']	= GoodsSale::getGoodsActivityMessage($value['sku']);
			//add by FDG367 495 【商城需求】商品图片上增加促销打标 2018-02-02 end

			if($platform == 1){
                $goods__list[$value['group_id']][] = $value;
            }else{
                $goods__list[] = $value;
            }
        }
        return $goods__list;
    }

    /**
     * @param获取首页限时抢购商品
     * $platform 平台1：pc；2wap  $limit n条数据
     * @return \yii\db\ActiveQuery
     */
    public function getIndexHotGoodsList($limit = 4){
        //获取热卖商品的配置
        $SaleConfiginfo = HotSaleConfig::getWapIndexHotSaleList(1);

        $mainGoodsInfo = BannerConfig::getHotList(empty($SaleConfiginfo['id']) ? 0 : $SaleConfiginfo['id'],self::TYPE_TWO);

        $sku_ids = count($mainGoodsInfo) > 0 ? array_filter(array_column($mainGoodsInfo, 'sku')) : [];

        if (count($sku_ids) > 0){
            $goods_list = GoodsSale::find()->alias('g')
				//mod by FDG367 2018-04-11 572 【商城需求】Mobile首页整体调整 商品打标图片不显示问题 start
				->select('g.marking_img_id,g.goods_pic_title,g.goods_pic_alt,g.main_goods_id,g.goods_name_cn,g.goods_main_pic,g.brand_name,g.identification_id,g.series,g.hidden_price,g.market_price,g.sale_price,g.unit,b.country_icon')
				//mod by FDG367 2018-04-11 572 【商城需求】Mobile首页整体调整 商品打标图片不显示问题 end
				->where(['g.sku'=>$sku_ids])
                ->leftJoin('j_goods_brand as b','b.id = g.brand_id')->limit($limit)->asArray()->all();
        }else{
            $goods_list = array();
        }
        return $goods_list;
    }

	/**
	 * @param获取图片的配置信息 FDG367 20171103
	 * @return \yii\db\ActiveQuery
	 */
	public function getHotSaleConfig(){
		return $this->hasMany(HotSaleConfig::className(),['id'=>'group_id']);
	}

	/**
	 * @parma add by FDG367  所有项目572 【商城需求】Mobile首页整体调整 2018-04-10 start	 * @params 获取对应热卖商品main_goods_ids 集合
	 * @param array $ids HotSaleConfig表中ids集合
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static  function getHotMainGoodsIds($ids=[]){

		$hotConfigInfo	= self::getHotList($ids);

		//获取商品的大类
		$genreTypes		= array_filter(array_column($hotConfigInfo,'category_id'));

		//获取商品的sku
		$skus			= array_filter(array_column($hotConfigInfo,'sku'));

		//如果有大类信息则查出对应的main_goods_id
		return GoodsSale::getMainGoodsIdInfo($genreTypes,$skus);
	}

	/**
	 * @param $ids banner_config ids
	 * @param int $type
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getHotList($ids,$type=''){
		//mod by FDG367 2018-04-21 572 【商城需求】Mobile首页整体调整 start
		$filterWhere	= array_filter(['group_id'=>$ids,'type'=>$type]);
		//mod by FDG367 2018-04-21 572 【商城需求】Mobile首页整体调整 end
		return self::find()->where($filterWhere)->asArray()->all();
	}



}